        $(function () {
				$.ajaxSetup({
					cache: false,
					headers: {
						"header":localStorage.getItem("token")
					}
				});
            $("#tt").datagrid({
            	idField : 'id',
            	title: "",
                url : basePath + "bloodSugar/page",
                width: "100%",  
                height: "550px",
                singleSelect : false, /* 是否选中一行 */
                pagination : true,/* 是否显示下面的分页菜单 */
                border:true,
                rowStyler: function() {
                	　　return 'height: 60px';
                	},
				onClickRow: function(index,row){
					$("#tt").datagrid("clearSelections");
				},
                rownumbers:true,
                columns : [ [  {field : 'id', checkbox: false,hidden:true},
								{field : 'measureTime',title : '测量时间',width : '15%',align : 'center'},
								{field : 'week',title : '星期',width : '10%',align : 'center'},
								{field : 'timePeriod',title : '时间段',width : '10%',align : 'center'},
                    		   {field : 'bloodSugarValue',title : '血糖值(mmol/L)',width : '12%',align : 'center'},
								{field : 'level',title : '水平',width : '6%',align : 'center',
									formatter:function(value,row,index){
										if(value=="偏高"){
											return "<span style='color: red'>" + value + "&#8593</span>";
										}else if(value=="偏低"){
											return "<span style='color: black'>" + value + "&#8595</span>";
										}else{
											return "<span style='color: green'>" + value + "-</span>";
										}
									}
								},
								{field : 'bloodSugarException',title : '血糖异常',width : '7%',align : 'center'},
								{field : 'remark',title : '备注',width : '15%',align : 'center'},
								{field : 'op',title : '操作',width : '20%',align : 'center',
								formatter:function(value,row,index){
									var id = row.id.toString();
									//var apply = "<a class='fonta' href='#' " + "onclick=toUseMedicineDlg(" + row.id +");>查看当日用药<a/>";
									var apply = '<a href="#" onclick="toUseMedicineDlg(\'' + id + '\')">查看当日用药</a>';
									var toDelete = '<a href="#" onclick="doDelete(\'' + id + '\')">删除</a>';
									var blank = "&nbsp;&nbsp;|";
									return apply + blank + toDelete;
								}
					}

                ]],
            	loadMsg : '数据加载中,请稍候......'
            }); 
  
            var p = $('#tt').datagrid('getPager');  
            $(p).pagination({  
                /*
				 * 页数文本框前显示的汉字 修改每页默认条数 搜索pageList在jquery.easyui.min.js中修改，
				 * 分页区下拉分页数量集合和默认每页分页条数 striped属性 交替行换色
				 */  
                beforePageText: '第',  
                afterPageText: '页    共 {pages} 页',  
                displayMsg: '当前显示 {from}-{to} 条记录,共 {total} 条记录'  
            });

        }
        );
        // 搜索
        function doSearch() {
            $('#tt').datagrid('load', {  
            	measureTime1: $('#measureTime1').val(),
				measureTime2: $('#measureTime2').val(),
				timePeriod:$('#timePeriod').val(),
				level:$('#level').val()
			});
        }
        
        function clearQry() {
			//$("#measureTime1").datebox("setValue","");
			//$("#measureTime2").datebox("setValue","");
			$("#qryForm")[0].reset();
			$("[name='measureTime1']").val("");
			$("[name='measureTime2']").val("");
			$("[name='timePeriod']").val("");
			$("[name='level']").val("");
			doSearch();
		}

        // 新增框修改框弹出
        function add(){
        	var title = "新增";
            $('#addBloodSugarDlg').css("top","0").dialog({
                title: title,
                width: 800,
                height: 410,
                closed: false,
                cache: false,
                href: basePath + 'navigator?page=/bloodSugar/add',
                modal: true
            });       	
        }

        // 新增
        function submitAddBloodSugar(){
			//setDisable("addBloodSugarBtn");
			var v = $("#applyff").form("validate");//验证通过为true
			if(!v){
				$.messager.alert('错误提示',"请填写完整信息!",'warning');
				return;
			}

			$.ajax({
				type:"post",
				url:basePath + "bloodSugar/add",
				beforeSend: function(request) {
					request.setRequestHeader("header", localStorage.getItem("token"));
				},
				data:$('#applyff').serializeArray(),
				cache:false,
				dataType: "json",
				success:function(result) {
					if(result.result.indexOf("已存在") != -1) {
						$.messager.alert('ERROR',result.result,'error');
					} else {
						$.messager.show({
							title:'提示',
							msg:result.result,
							timeout:5000,
							showType:'slide'
						});
						cancelDlg1("addBloodSugarBtn");
						$('#tt').datagrid('reload');// 刷新
					}
				},
				error: function (result) {
					$.messager.alert('ERROR',JSON.stringify(result),'error');
				}
			});
        }


		function setDisable(id){
			//$("#"+id).css({'background-color' : 'gray'});
			$("#"+id).attr("disabled", true);
		}

		function setAvailable(id){
			$('#'+id).removeAttr('disabled');
		}

        // 删除
        function doDelete(id) {
        	$.messager.confirm('提示', '你确定要删除该记录吗?',function(r){
        		if(r){
        			$.ajax({
                        type:"delete",
                        url:basePath + "bloodSugar/delete/"+id,
                        cache:false,
                        success:function(result) {
                         	$.messager.show({
                         		title:'提示',
                         		msg:'删除成功',
                         		timeout:5000,
                         		showType:'slide'
                         	});
                        	$('#tt').datagrid('reload');// 刷新
                        	rows.length=0;
                        }
                    });
        		}

        	})
        }

        // 关闭弹窗
        function cancelDlg1(){
        	$('#addBloodSugarDlg').dialog("close");
        }

        //关闭弹窗
        function cancelDlg(dlgName){
        	$('#'+dlgName).dialog("close");
        }
        // 判断是否为空
        function isNull( str ){
        	if ( str == "" ) return true;
        	var regu = "^[ ]+$";
        	var re = new RegExp(regu);
        	return re.test(str);
        }


		function toUseMedicineDlg(id) {
			$("#datagrid").datagrid({
				url : basePath + "useMedicine/findByBloodSugarId?bloodSugarId=" + id,
				iconCls: "icon-add",
				width: "850px",
				height: "300px",
				fitColumns: false,
				loadMsg: "数据加载中......",
				pagination: false,
				rownumbers: true,
				nowrap: false,
				showFooter: false,
				singleSelect: false,
				columns : [ [
					{field : 'timePeriod',title : '时间段',width : '10%',align : 'center'},
					{field : 'content',title : '用量',width : '30%',align : 'left'},
					{field : 'remark',title : '备注',width : '45%',align : 'left'}
				]],
				onClickRow: function(index,row){
					$("#datagrid").datagrid("clearSelections");
				},
				pageList: [10]
			});


			$('#dlg').window('open');  //弹出这个dialog框

		}
		
		function exportBloodSugar() {
			window.location.href = basePath+"bloodSugar/export";
		}